package main

import (
	"fmt"
	"io"
	"log"
)

func main() {
	trace("function1")
	defer un("function1")
	function1()
	a()
	f()
	f1("GO")
}

func function1() {
	fmt.Printf("In fuction1 at the top\n")
	defer function2()
	fmt.Printf("In fuction1 at the bottom\n")
}

func function2() {
	fmt.Printf("In fuction2\n")
}

func a() {
	i := 0
	defer fmt.Println(i)
	i++
	return
}
func f() {
	for i := 0; i < 10; i++ {
		defer fmt.Println(i)
	}
}

// defer实现代码追踪
func trace(s string) {
	fmt.Println("entering:", s)
}

func un(s string) {
	fmt.Println("leaving:", s)
}

func f1(s string) (n int, err error) {
	defer func() {
		log.Printf("f1(%q) = %d, %v", s, n, err)
	}()
	return 7, io.EOF
}
